<?php
/**
 * Created by PhpStorm.
 * User: xcx
 * Date: 2017/6/2
 * Time: 10:17
 */

class Controller_Adminmall_Shop extends Controller_Adminmall_Abstract
{
    private $DB_SERVER = "xhs_slave";
    /*
     * 门店列表
     */
    public function listAction(){



        $page = FRequest::getInt("page");
        $phone = FRequest::getString("phone") ? FRequest::getString("phone") : "";
        $name = FRequest::getString("name") ? FRequest::getString("name") : "";
        $city = FRequest::getString("city") ? FRequest::getString("city") : "";

        $where = array();



        if($city){
            $where['c.city'] = array("like" => "%{$city}%");
        }

        if($name){
            $where['c.name'] = array("like" => "%{$name}%");
        }

        if($phone){
            $where['c.phone'] = $phone;
        }

        $where['c.status'] = 1;

        $userTable = new FTable("chain_shop", 'c', $this->DB_SERVER);
        $list = $userTable->fields("c.*, m.title as title")
            ->leftJoin("manager_group", 'm', "c.gid=m.id")
            ->where($where)
            ->order("id desc")
            ->limit(20)
            ->page($page)
            ->select();

        $pagerInfo = $userTable->getPagerInfo();
        $this->assign('page_info', $pagerInfo);
        $this->assign("oss_url", FConfig::get("global.s_url_oss")."/uploads/");
        $this->assign("data", $list);
        $this->display('shop/shop-list');
    }

    /*
     * 添加门店
     */
    public function modifyAction(){
        global $_F;
//        $this->openDebug();
        $id = FRequest::getInt("id");
        if($this->isPost()){



            $id = FRequest::getPostInt('id');
            $gid = FRequest::getPostInt('gid');
            $name = FRequest::getPostString("name");
            $address = FRequest::getPostString("address");
            $city = FRequest::getPostString("city");
            $phone = FRequest::getPostString("phone");
            $x = FRequest::getPostString("x");
            $y = FRequest::getPostString("y");
            $manager = FRequest::getPostString("manager");
            $status = FRequest::getPostInt("status");
            $money = FRequest::getPostString("money") ? FRequest::getPostString("money") : 0;
            $stop_work_time = FRequest::getPostString("stop_work_time") ? FRequest::getPostString("stop_work_time") : "";
            $start_work_time = FRequest::getPostString("start_work_time") ? FRequest::getPostString("start_work_time") : "";
            $work_status = FRequest::getPostInt("work_status");
            $save = array(
                "name" => $name,
                "gid" => $gid,
                "address" => $address,
                "city" => $city,
                "phone" => $phone,
                "x" => $x,
                "y" => $y,
                "manager" => $manager,
                "money" => $money,
                "status" => $status,
                "work_status" => $work_status,
                "stop_work_time" => $stop_work_time,
                "start_work_time" => $start_work_time,
            );

            //图标
            if (!empty($_FILES["shop_img"]['name'])) {
                $img2_url = Service_Attach::getInstance()->uploadOSS('shop_img','xhs/goods','jpg');
                $save['shop_img'] = $img2_url;
            }


            $table = new FTable("chain_shop", 'c', 'xhs_master');
            if($id){
                $table->update1($save, array("id" => $id));
                $this->success("修改成功", $_F['refer']);
            }else{
                $table->insert($save);
                $this->success("添加成功", $_F['refer']);
            }

        }else{
            $table =  new FTable("chain_shop", 'c', $this->DB_SERVER);
            $data = $table->fields("*")->where(array("id" => $id))->find();
            $this->assign("data", $data);
        }

        $mTable = new FTable("manager_group", '', $this->DB_SERVER);
        $mList = $mTable->select();

        $workStatus = FConfig::get("type.work_status");
        $this->assign("workStatus", $workStatus);
        $this->assign("oss_url", FConfig::get("global.s_url_oss")."/uploads/");
        $this->assign('mList', $mList);
        $this->display("shop/shop-modify");
    }

    public function lookAction(){
        global $_F;
        $shop_id = $_F['member']['shop_id'];
        if($shop_id<=0) $this->error('店铺不存在');
        $table =  new FTable("chain_shop", 'c', $this->DB_SERVER);
        $data = $table->fields("*")->where(array("id" => $shop_id))->find();
        $this->assign("data", $data);
        $this->display("shop/shop-look");
    }



    /*
     * 百度Api
     */
    public function bdAction(){
        $this->display("shop/bd");
    }

    /*
     * 删除门店
     */
    public function delAction(){
        global $_F;
        $id = FRequest::getInt("id");
        if($id){
            $table = new FTable("chain_shop", "", 'xhs_master');
            $table->update1(array('status' => -1), array("id" => $id));
            $this->success("删除成功", $_F['refer']);
        }else{
            $this->error('id为空', $_F['refer']);
        }
    }

    /**
     * 店员管理
     */
    public function clerkAction(){
        global $_F;
        $shop_id = $_F['member']['shop_id'];
        $where = array();
        if($_F['member']['isadmin'] != 1 && $shop_id>0) $where['shop_id'] = $shop_id;
        $table = new FTable("clerk", "t1", $this->DB_SERVER);
        $page = FRequest::getInt('page');
        $dataList = $table->fields('t1.*,t2.name as shop_name')
                            ->leftJoin('chain_shop','t2','t1.shop_id=t2.id')
                            ->where($where)->page($page)->limit(20)->select();
        $this->assign('dataList',$dataList);

        $pagerInfo = $table->getPagerInfo();
        $this->assign('page_info', $pagerInfo);
        $this->display("shop/clerk");
    }

    /**
     * 修改店员信息
     */
    public function clerkModifyAction(){
        global $_F;
        $isadmin = $_F['member']['isadmin'];
        $uid = FRequest::getRequestInt("uid");
        if($this->isPost()){
            $table = new FTable("clerk", "", 'xhs_master');
            $data['username'] = FRequest::getPostString('username');
            $data['phone'] = FRequest::getPostString('phone');
            $data['status'] = FRequest::getPostInt('status');
            if($isadmin == 1) $data['shop_id'] = FRequest::getPostInt('shop');
            $where['uid'] = $uid;
            $shop_id = $_F['member']['shop_id'];
            if($shop_id>0) $where['shop_id'] = $shop_id;

            $result = $table->update1($data, $where);
            if($result)$this->success("修改成功", $_F['refer']);
            else $this->error('修改失败', $_F['refer']);
        }

        $table = new FTable("clerk", "t1", $this->DB_SERVER);
        $data = $table->fields('t1.*,t2.name as shop_name')
                        ->leftJoin('chain_shop','t2','t1.shop_id=t2.id')->where(array('t1.uid'=>$uid))->find();

        if($isadmin == 1){
            $shopT = new FTable('chain_shop','',$this->DB_SERVER);
            $shopList = $shopT->fields('id,name,status')->order("FIELD(`status`,'1','2','-1')")->select();
            $this->assign('shopList',$shopList);
        }

        $this->assign('isadmin',$isadmin);
        $this->assign('data',$data);
        $this->display("shop/clerkModify");
    }

    /*
     * 删除店员
     */
    public function clerkDelAction(){
        global $_F;
        $uid = FRequest::getInt("uid");
        if($uid){
            $table = new FTable("clerk", "", 'xhs_master');
            $where['uid'] = $uid;
            $shop_id = $_F['member']['shop_id'];
            //if($_F['member']['isadmin'] != 1 && $shop_id>0) $where['shop_id'] = $shop_id;

            $user = $table->where($where)->find();
            FLogger::write('管理员ID: '. $_F['uid'],'delete_log');
            FLogger::write('删除店员: ' . json_encode($user),'delete_log');

            $result = $table->where($where)->remove(true);
            if($result)$this->success("删除成功", $_F['refer']);
            else $this->error('删除失败', $_F['refer']);
        }else{
            $this->error('uid为空', $_F['refer']);
        }
    }

    /**
     * 专属二维码
     */
    public function qrcodeAction(){
//        $this->openDebug();
        $shop_id = FRequest::getInt('id');
        $table =  new FTable("chain_shop",'',$this->DB_SERVER);
        $shop = $table->where(array('id'=>$shop_id))->find();
        $ticket = '';
        if(!empty($shop) && $shop['ticket']){
            $ticket = $shop['ticket'];
        }else{
            require_once(APP_ROOT . 'lib/weixin/WeixinChat.class.php');
            $options = array(
                'token' => FConfig::get('wechat.seller.token'),
                'appid' => FConfig::get('wechat.seller.appid'),
                'appsecret' => FConfig::get('wechat.seller.appsecret'),
            );
            $weixin = new WeixinChat($options);
            $result = $weixin->createQrcode($shop_id,1);
            if($result){
                $ticket = $result['ticket'];
                $url = $result['url'];
                $table->where(array('id'=>$shop_id))->update1(array('ticket'=>$ticket,'qr_url'=>$url));
            }else{
                FResponse::output(array('status'=>0,'msg'=>'生成失败'));
            }
        }

        FResponse::output(array('status'=>1,'msg'=>'生成成功','ticket'=>$ticket,
            'qr_url'=>'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($ticket)));
    }

}